<?php
class Ladder extends CI_Controller {

	private $NUM_GAMES;

	function __construct()
	{
		parent::__construct();

		$this->config->load('challenge_settings');

		$this->NUM_GAMES = $this->config->item('best_of');

	}
	
	/**
	 * Default landing page for ladder section.
	 * I Understand that we are overful but the srevice myst really be exceptional.
	 * 
	 * */
	function index($challenge_status = '') {
		$this->load->model('ladder_model');
		$this->load->library('table');
		$this->ladder_model->refresh_table();

		if ($challenge_status != ''){
			$params['challenge_status'] = $challenge_status;
		}
		$params['subview'] = 'subview/ladder_view';
		$params['ladder_table'] = $this->ladder_model->get_table();
		$params['table_library'] = &$this->table;

		$this->load->view('homepage_view', $params);
	}

	//Issues a challenge for logged player to player posted in form
	function issue_challenge(){
		//Attempt to issue challenge. Legality will be checked in challenge object
		$game_set_data = array(
			'player1' => $this->session->userdata('account'),
			'player2' => $this->input->post('target'),
			'number_games' => $this->NUM_GAMES,
			'game_type' => 'ladder'
			);

			$challenge = $this->challenge_object->create_challenge($game_set_data);

			if ($challenge == null){
				$challenge_status = "Challenge Already in Progress";

			} else {
				$challenge_status = "Challenge Issued";
			}
			$this->index($challenge_status);
	}

}